using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Chimera2.Classes;

namespace Chimera2.GUI
{
    public partial class Editors : Form
    {
        Classes.Editor editor = new Classes.Editor();
        List<Hub> hubs = new List<Hub>();
        List<Bulletin> bulletins = new List<Bulletin>();

        // define delegate
        public delegate void PassControl(object sender);

        // create instance (null)
        public PassControl passControl;

        public Editors()
        {
        }

        public Editors(Classes.Editor _editor, Dictionary<Guid, Hub> _hubs, Dictionary<Guid, Bulletin> _bulletins)
        {
            InitializeComponent();
            editor = _editor;
            hubs = new List<Hub>(_hubs.Values);
            bulletins = new List<Bulletin>(_bulletins.Values);
            OnLoad();
        }

        private void OnLoad()
        {
            txtUserName.Text = editor.UserName;

            foreach (Hub hub in hubs)
            {
                if (editor.AccessFrom.Contains(hub.Guid))
                {
                    clbAllowAccessFromList.Items.Add(hub.HubName, true);
                }
                else
                {
                    clbAllowAccessFromList.Items.Add(hub.HubName, false);
                }
            }

            rdbAllowAccessToGlobal.Checked = editor.GlobalAccess;
            rdbAllowAccessToList.Checked = (!editor.GlobalAccess);
            clbAllowAccessToList.Enabled = (!editor.GlobalAccess);

            foreach (Bulletin bulletin in bulletins)
            {
                if (editor.AccessTo.Contains(bulletin.Guid))
                {
                    clbAllowAccessToList.Items.Add(bulletin.BulletinName, true);
                }
                else
                {
                    clbAllowAccessToList.Items.Add(bulletin.BulletinName, false);
                }
            }

            chkCanRemoveItems.Checked = editor.CanRemove;
            chkCanEditItems.Checked = editor.CanEdit;
            chkCanMoveItems.Checked = editor.CanMove;
        }

        private void OnSave()
        {
            editor.UserName = txtUserName.Text;

            editor.AccessFrom.Clear();
            foreach (int index in clbAllowAccessFromList.CheckedIndices)
            {
                editor.AccessFrom.Add(hubs[index].Guid);
            }

            editor.AccessTo.Clear();
            foreach (int index in clbAllowAccessToList.CheckedIndices)
            {
                editor.AccessTo.Add(bulletins[index].Guid);
            }

            editor.GlobalAccess = rdbAllowAccessToGlobal.Checked;
            editor.CanRemove = chkCanRemoveItems.Checked;
            editor.CanEdit = chkCanEditItems.Checked;
            editor.CanMove = chkCanMoveItems.Checked;


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("'UserName' is required.", "Editor", MessageBoxButtons.OK);
            }
            else
            {
                OnSave();
                passControl(editor);
                this.Close();
            }
        }

        private void rdbAllowAccessToList_CheckedChanged(object sender, EventArgs e)
        {
            clbAllowAccessToList.Enabled = rdbAllowAccessToList.Checked;
        }
    }
}